<?php

namespace app\admin\model;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Task extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'task';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'state_text',
        'admin_id_text',
        'assign_id_text',
        'project_ids_text',
        'createtime_text',
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1')];
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAdminIdTextAttr($value, $data){
        $str = Admin::where('id',$data['admin_id'])->value('nickname');
        return $str?$str:'-';
    }


    public function getAssignIdTextAttr($value, $data){
        $str = Admin::where('id',$data['assign_id'])->value('nickname');
        return $str?$str:'-';
    }


    public function getProjectIdsTextAttr($value,$row){

        $arr_value = explode(',',$row['project_ids']);
        foreach($arr_value as $v){
            $arr[] = Project::where('id',$v)->value('title');
        }
        return implode(',',$arr);
    }

    public function getCreatetimeTextAttr($value,$row){
        return date('Y-m-d H:i:s',$row["createtime"]);
    }

    //批量插入
    public function batcInsert($assign_ids,$param){
        $self = new self;
        $assign_id_arr = explode(',',$assign_ids);
        //批量插入的数组
        $data = [];
        foreach ($assign_id_arr as $k => $v){
            $temp = [];
            $temp['title'] = $param['title'];
            $temp['admin_id'] = $param['admin_id'];
            $temp['assign_id'] = $v;
            $temp['project_ids'] = $param['project_ids'];
            $temp['content'] = $param['content'];
            $data[] = $temp;
        }
        Db::startTrans();
        try{
            $self->saveAll($data);
            Db::commit();
            return ['code'=>1,'msg'=>'批量插入成功','data'=>[]];
        }catch (Exception $e){
            Db::rollback();
            return ['code'=>0,'msg'=>'批量插入失败','data'=>[]];
        }
    }

    //获取信息
    public function getInfo($id,$admin_id=null,$assign_id=null,$project_id=null,$state=null,$order="weigh desc,id desc",$page=1,$pageCount=10) {
        $offset = ($page-1)*$pageCount;
        $where = [];
        if($id){$where['id'] = ['=',$id];}
        if($admin_id){$where['admin_id'] = ['=',$admin_id];}
        if($assign_id){$where['assign_id'] = ['=',$assign_id];}
        if($project_id){$where[] = ['exp',Db::raw("FIND_IN_SET($project_id,project_ids)")];}
        if($admin_id){$where['admin_id'] = ['=',$admin_id];}
        if($state||$state==="0"){$where['state'] = ['=',$state];}
        $info = collection(self::where($where)->order($order)->limit($offset,$pageCount)->select())->toArray();
//        dd(Task::getLastSql());
        return $info;
    }

}
